//
//  InfoList.m
//  Trading365
//
//  Created by yifei tong on 12-1-10.
//  Copyright (c) 2012年 dhc. All rights reserved.
//

#import "ApproveHisList.h"
#import "ApproveHisListCell.h"


@implementation ApproveHisList
@synthesize table;
@synthesize responseParams;
@synthesize table_name,oreder_id,workflow_id;

- (void)dealloc
{
    [table release]; 
    [table_name release];
    [oreder_id release];
    [workflow_id release];
    
    [responseParams release];
    [super dealloc];
}

- (id)init
{
	self = [super init];
    responseParams = [[NSMutableArray alloc] initWithCapacity:0];
    
	if(self)
	{
	}
	
	return self;
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [self init];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    getflag = YES;
    
    self.title = @"审批历史";
        
    [self refreshData];
    [self.table reloadData];
        
}

- (void)viewWillAppear:(BOOL)animated {

}

- (void)viewDidUnload
{
    [[[TaskManager Share] GetTaskByType:GetApproveHisList Delegate:self] cancel];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    table=nil;
}

- (void)viewDidAppear:(BOOL)animated
{

    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark - Table view data source
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [responseParams count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
    static NSString *CellIdentifier = @"ApproveHisListCell";
    
    ApproveHisListCell* cell = (ApproveHisListCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil){
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"ApproveHisListCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    
    // Configure the cell...
    cell.contentView.backgroundColor = [UIColor clearColor];
    cell.backgroundColor = [UIColor clearColor];
    cell.backgroundView.backgroundColor = [UIColor clearColor];
   
    NSUInteger row = [indexPath row];
    if (row>[responseParams count]) {
        return cell;
    }
    
    NSMutableDictionary* dic = [[NSMutableDictionary alloc] initWithDictionary:[responseParams objectAtIndex:row]] ;

    cell.msg_subject.text= [dic objectForKey:@"approval_opinion"];
    cell.msg_user.text= [dic objectForKey:@"approval_user"];
    cell.msg_time.text= [dic objectForKey:@"approval_time"];
    
    cell.after_state_name.text= [dic objectForKey:@"after_state_name"];
    NSString *agree1=[dic objectForKey:@"agree"];
    NSString *agree=INFO_STRING_APPROVE_AGREE0;
    //说明 agree= '0':'驳回','1':'同意','3':'提交审批'
    if ([agree1 isEqualToString:@"3"]){
        agree=INFO_STRING_APPROVE_AGREE3;
    }
    if ([agree1 isEqualToString:@"1"]){
        agree=INFO_STRING_APPROVE_AGREE1;
    }
    
    cell.state_name.text= agree;
    
    cell.before_state_name.text= [dic objectForKey:@"before_state_name"];
    
    [dic release];
    
    return cell;

}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}


#pragma mark - UserTask

-(void) OnPreExcute
{
    [super OnPreExcute];
}

-(void) OnPostExcute
{    
    [super OnPostExcute];
}

-(void) DoInBackground
{
    if (getflag == NO ) {
        return;
    }

    NSMutableArray *tmp=nil;
    XmlRpc *xml = [[XmlRpc alloc] init];

    tmp = [xml GetApproveHistoryList:oreder_id tableName:table_name WorkFlowId:workflow_id];       
    
    for (NSMutableDictionary* itq in tmp) {
        [responseParams addObject:itq];
    }
        
    if ([tmp count]<PER_PAGE_ROWS) {
        getflag = NO;
    }
    
    [xml release];

    [self.table reloadData];
}

#pragma mark - my function

-(void)refreshData
{    
    [[[TaskManager Share] GetTaskByType:GetApproveHisList Delegate:self] Excute];
}

-(void) getMoreData
{
    [[[TaskManager Share] GetTaskByType:GetApproveHisList Delegate:self] Excute];
}

@end
